﻿/***************************************************************
 *Copyright(c) 2014-2015 Company By LH Unigress
 *All rights reserved.
 *文件名称：付款管理文件
 *简要描述：计算顾客购买商品的价格，以及给顾客的找零操作
 *
 *当前版本：V2.0
 *作者：Kelvin Li
 *创作日期：2014/10
 *说明：主要用于显示顾客购买商品的总额，进行消费显示和计算，将销售记录写进销售记录数据表中
 **************************************************************************
 *更新说明：
 *当前版本：V3.0
 *修改时间：2015-8-4 17:04:37
******************************************************************/
#include "paywidget.h"
#include "iconhelper.h"
#include "myhelper.h"
#include "ui_paywidget.h"
#include "commonhelper.h"

#include <QMouseEvent>

PayWidget::PayWidget(QWidget* parent)
    : QDialog(parent)
    , ui(new Ui::PayWidget)
{
    ui->setupUi(this);
    this->initForm();
    this->initParameters();
}

PayWidget::~PayWidget()
{
    delete ui;
}

void PayWidget::initForm()
{
    this->setWindowFlags(Qt::FramelessWindowHint);
    this->setWindowTitle("付款结算");
//    ui->lab_Title->setText("付款结算");

    //安装事件监听器,让标题栏识别鼠标双击
    ui->lab_Title->installEventFilter(this);
    IconHelper::Instance()->SetIcon(ui->btnMenu_Close, QChar(0xf00d), 10);
    IconHelper::Instance()->SetIcon(ui->lab_Ico, QChar(0xf00a), 12);

    mousePressed = false;
}

void PayWidget::initParameters()
{
    ui->lineEditFavorable->setText("0.00");
    ui->radioButtonStaffCard->setChecked(true);
    on_radioButtonStaffCard_clicked();
}

void PayWidget::setCheckInfo(QString total, QString discount)
{
    ui->lineEditShouldPay->setText(total);
    ui->lineEditStaffCard->setText(total);
    ui->lineEditFavorable->setText(discount);
    ui->lineEditOriginalPrice->setText(QString::number(total.toFloat() - discount.toFloat()));
}

QString PayWidget::getPayType()
{
    if (ui->radioButtonCash->isChecked()) {
        return QString("cash");
    } else if (ui->radioButtonStaffCard->isChecked()) {
        return QString("staffcard");
    } else if (ui->radioButtonWechat->isChecked()) {
        return QString("wechat");
    }
}

float PayWidget::getActuallyPay()
{
    return ui->lineEditActuallyPay->text().toFloat();
}

float PayWidget::getChange()
{
    return ui->lineEditChange->text().toFloat();
}

void PayWidget::mouseMoveEvent(QMouseEvent* e)
{
    if (mousePressed && (e->buttons() && Qt::LeftButton)) {
        this->move(e->globalPos() - mousePoint);
        e->accept();
    }
}

void PayWidget::mousePressEvent(QMouseEvent* e)
{
    if (e->button() == Qt::LeftButton) {
        mousePressed = true;
        mousePoint = e->globalPos() - this->pos();
        e->accept();
    }
}

void PayWidget::mouseReleaseEvent(QMouseEvent*)
{
    mousePressed = false;
}

void PayWidget::on_btnMenu_Close_clicked()
{
    this->close();
}

/*
 *结账计算
*/
/**
 * @brief PayWidget::on_pbnOk_clicked 确认结算后，发出结算信号给父控件处理
 */
void PayWidget::on_pbnConfirm_clicked()
{
    QString actually = ui->lineEditActuallyPay->text();
    QString should = ui->lineEditShouldPay->text();
    if (actually.toFloat() < should.toFloat()) {
        myHelper::ShowMessageBoxError("实际收款【" + actually + "】小于应收款【" + should + "】X_X 请检查输入!");
    } else {
        emit confirmPayment();
        this->close();
    }
}

void PayWidget::on_radioButtonCash_clicked()
{
    ui->lineEditCash->setEnabled(true);

    ui->lineEditStaffCard->setEnabled(false);
    ui->lineEditCredit->setEnabled(false);
    ui->lineEditWechat->setEnabled(false);
}

void PayWidget::on_radioButtonStaffCard_clicked()
{
    ui->lineEditStaffCard->setEnabled(true);

    ui->lineEditCredit->setEnabled(false);
    ui->lineEditWechat->setEnabled(false);
    ui->lineEditCash->setEnabled(false);
}

void PayWidget::on_radioButtonWechat_clicked()
{
    ui->lineEditWechat->setEnabled(true);

    ui->lineEditStaffCard->setEnabled(false);
    ui->lineEditCredit->setEnabled(false);
    ui->lineEditCash->setEnabled(false);
}

/**
 * @brief PayWidget::on_lineEditStaffCard_textChanged 找零钱值的实时计算
 * @param arg1
 */
void PayWidget::on_lineEditStaffCard_textChanged(const QString& arg1)
{
    Q_UNUSED(arg1);
    ui->lineEditActuallyPay->setText(ui->lineEditStaffCard->text());

    //进行计算
    qreal cash = ui->lineEditStaffCard->text().toFloat();
    qreal change = cash - ui->lineEditShouldPay->text().toFloat();
    ui->lineEditChange->setText(QString::number(change));
}
